package sort;

import java.util.Arrays;

/**
 * 冒泡排序
 */
public class BubbleSort {
  public static void main(String[] args) {
    int[] array = {2,1,3,4,5};

    int temp=0;
    boolean flag = false;//表示某次排序是否有交换操作
    for (int i = 0; i < array.length-1; i++) {

        for (int j = 0; j < array.length-1-i; j++) {
            if (array[j]>array[j+1]){
                flag=true;
                temp=array[j];
                array[j]=array[j+1];
                array[j+1]=temp;
            }
        }
      System.out.println("第"+(i+1)+"次排序");
      System.out.println(Arrays.toString(array));
      if (!flag){
          break;
      }else {
          flag = false;
      }

    }
  }
}
